/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       TcpProxyController.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/12/8      feiwe                         add
 */


package com.fengwei.controller;

import com.fengwei.config.TcpProxyConfig;
import com.fengwei.startup.InitTcpProxyRunner;
import com.fengwei.util.TransPortData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.ServerSocket;
import java.net.Socket;

@Controller
public class TcpProxyController {
    private final Logger logger = LoggerFactory.getLogger(InitTcpProxyRunner.class);

    @Autowired
    private TcpProxyConfig tcpProxyConfig;

    private TransPortData transPortDataLocalToRemote;
    private TransPortData transPortDataRemoteToLocal;

    private ServerSocket serverSocket;

    @GetMapping(value = "/tcpProxy/0")
    public String tcpProxy0(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            //获取本地监听端口、远程IP和远程端口
            int localPort = 3300;
            String remoteIp = "101.231.60.125";
            int remotePort = 10038;

            //启动本地监听端口
            serverSocket = new ServerSocket(localPort);
            logger.error("localPort=" + localPort + ";remoteIp=" + remoteIp +
                    ";remotePort=" + remotePort + ";启动本地监听端口" + localPort + "成功！");
            while (true) {
                Socket clientSocket = null;
                Socket remoteServerSocket = null;
                try {
                    if (null == serverSocket) {
                        break;
                    }
                    //获取客户端连接
                    clientSocket = serverSocket.accept();
                    logger.error("accept one client");
                    //建立远程连接
                    remoteServerSocket = new Socket(remoteIp, remotePort);

                    logger.error("create remoteip and port success");
                    //启动数据转换接口
                    transPortDataLocalToRemote = (new TransPortData(clientSocket, remoteServerSocket, "1"));
                    transPortDataLocalToRemote.start();
                    transPortDataRemoteToLocal = (new TransPortData(remoteServerSocket, clientSocket, "2"));
                    transPortDataRemoteToLocal.start();
                } catch (Exception ex) {
                    logger.error("", ex);
                }
                //建立连接远程
            }


        } catch (Exception e) {
            logger.error("", e);
        }

        return "0";
    }

    @GetMapping(value = "/tcpProxy/1")
    public String tcpProxy1(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        new Socket("localhost", 3300);
        transPortDataLocalToRemote.destroy();
        transPortDataRemoteToLocal.destroy();
        serverSocket.close();
        serverSocket = null;
        return "1";
    }
}
